package cn.wen233.orderconsumer.api;

import cn.wen233.common.api.R;
import cn.wen233.common.infrustructure.exception.BaseException;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常处理 api
 *
 * @author wenei
 * @date 2021-06-08 8:54
 */
@RestControllerAdvice
public class ExceptionApi {

    private static final Logger log = LoggerFactory.getLogger(ExceptionApi.class);

//    @ExceptionHandler(Exception.class)
//    public R<?> baseException(Exception e) {
//        return R.failure(e.getMessage());
//    }

    /**
     * 熔断异常处理 处理在回退工厂中抛出的异常
     * @param e 熔断异常
     * @return Rest风格数据包装
     */
    @ExceptionHandler(HystrixRuntimeException.class)
    public R<?> exceptionHandler(HystrixRuntimeException e) {
        Throwable cause = e.getFallbackException();
        while (cause.getCause() != null) {
            cause = cause.getCause();
        }
        log.warn("拦截的回退异常信息：", cause);
        return R.failure(cause.getMessage(), R.Status.FAILURE);
    }
}
